import { FREQUENCY } from '@/const';
import { checkFileType } from '@/tools/file';
import type { CareServiceDetail, CareServiceFormState, ServiceTimeForm, ServiceTimeSubmit } from '@/types';
// import { message } from 'ant-design-vue';
import type { LevelItem, ServiceContentConfig, ServiceTagConfig, ServiceTimeDetail } from '@/types';
import type { Rule } from 'ant-design-vue/es/form';
import { isEmpty, pick } from "radash";

export const transformServiceTimeToFind = (find?: LevelItem) => {
	const init: ServiceTimeForm = {
		frequency: [FREQUENCY.day],
		month: [],
		week: [],
		everyMonth: 'none',
		everyWeek: 'none',
		time: [],
		first: '',
	};
	if (find) {
		const data = pick(find, ['frequency', 'month', 'week', 'everyMonth', 'everyWeek', 'time', 'first']);
		Object.assign(init, data);
	}
	return init;
};

export const transformServiceTimeToInit = (props?: ServiceTimeForm) => {
	const init: ServiceTimeForm = {
		frequency: [FREQUENCY.day],
		month: [],
		week: [],
		everyMonth: 'none',
		everyWeek: 'none',
		time: [],
		first: '',
	};
	if (props) {
		const data = pick(props, ['frequency', 'month', 'week', 'everyMonth', 'everyWeek', 'time', 'first']);
		Object.assign(init, data);
	}
	return init;
};

export const transformServiceTimeToForm = (data?: ServiceTimeDetail) => {
	const init: ServiceTimeForm = {
		frequency: [FREQUENCY.day],
		month: [],
		week: [],
		everyMonth: 'none',
		everyWeek: 'none',
		time: [],
		first: '',
	};
	if (!data) return init;
	if (data?.Moon?.Days?.length) {
		init.frequency = [FREQUENCY.month];
		init.month = data.Moon.Days || [];
	} else if (data?.Week?.Days?.length) {
		init.frequency = [FREQUENCY.week];
		init.week = data.Week.Days || [];
	} else if (data?.Others?.First?.length) {
		init.frequency = [FREQUENCY.other];
		if (data?.Others?.Moon) {
			init.everyMonth = data.Others.Moon;
		} else {
			init.everyWeek = data.Others.Week;
		}
		init.first = data.Others.First || '';
	}
	if (data?.Time?.length) {
		init.time = [data.Time[0].Start, data.Time[0].End];
	}
	return init;
};

export const transformServiceTimeToSubmit = (form: ServiceTimeForm) => {
	const init: ServiceTimeSubmit = {
		cron: '',
		moon: {
			days: [],
		},
		others: {
			first: '',
			moon: '',
			week: '',
		},
		time: [],
		week: {
			days: [],
		},
	};
	if (form.frequency[0] === FREQUENCY.week) {
		init.week.days = form.week || [];
		// init.week.days = [];
	} else if (form.frequency[0] === FREQUENCY.month) {
		init.moon.days = form.month || [];
	} else if (form.frequency[0] === FREQUENCY.other) {
		if (form.everyMonth) {
			init.others.moon = form.everyMonth || '';
		} else {
			init.others.week = form.everyWeek || '';
		}
		init.others.first = form.first || '';
	}
	if (form?.time?.length) {
		init.time = [
			{
				start: form.time[0],
				end: form.time[1],
			},
		];
	}
	return init;
};

/**
 * @description: 添加照护配置表单参数转换
 * @return {*}
 */
export const transformFormToSubmit = (form: CareServiceFormState) => {
	const pictures = [];
	let video = '';
	for (const v of (form.case || [])) {
		if (checkFileType(v.url) === 'img') {
			pictures.push(v.url);
		}
		if (checkFileType(v.url) === 'video') {
			video = v.url;
		}
	}
	return {
		careLevels: form.levelsInfo.map((v) => ({
			careLevel: Number(v.id),
			careWay: Number(v.way?.[0] || 0),
			desc: v.desc,
			serviceTime: transformServiceTimeToSubmit(v.serviceTime),
			// ...transformServiceTimeToSubmit(v),
		})),
		icon: form.icon,
		name: form.name,
		pictures,
		video,
		tagID: Number(form.tag?.[0]),
	};
};

/**
 * @description: 照护配置表单参数转换前端渲染
 * @return {*}
 */
export const transformDetailToForm = (data?: CareServiceDetail) => {
	if (!data) return {};
	const videoList = data.Video ? [{
		url: data.Video,
	}] : [];
	const levelsInfo = data.Levels?.map((v) => {
		const timeJson = transformServiceTimeToForm(v.ServiceTime);
		return {
			id: v.ID,
			way: isEmpty(v.Way?.ID) ? [] : [v.Way.ID],
			desc: v.Desc,
			serviveTime: timeJson,
			...timeJson,
		}
	}) || [];
	return {
		levelsInfo,
		levels: isEmpty(data.Levels) ? [] : data.Levels?.map((v) => String(v.Level?.ID)).filter(Boolean),
		icon: data.Icon,
		name: data.Name,
		tag: isEmpty(data?.Tag?.ID) ? [] : [data.Tag.ID],
		case: (data.Pictures || []).filter(url => checkFileType(url) === 'img').map((url: string) => ({
			url,
		})).concat(videoList),
		video: data.Video || '',
	};
};

export const checkServiceTime = async (_: Rule, value: ServiceTimeForm) => {
	// return Promise.resolve();
	if (!value?.frequency?.length) {
		return Promise.reject('请选择服务时间');
	}
	if (value.frequency[0] === FREQUENCY.week && !value?.week?.length) {
		return Promise.reject('请选择每周时间');
	}
	if (value.frequency[0] === FREQUENCY.month && !value?.month?.length) {
		return Promise.reject('请选择每月时间');
	}
	if (value.frequency[0] === FREQUENCY.other) {
		if (!value?.first?.length) {
			return Promise.reject('请选择首次执行时间');
		}
		if (value?.everyMonth === 'none' && value?.everyWeek === 'none') {
			return Promise.reject('请选择每月或者每周');
		}
	}
	if (!value?.time?.length) {
		return Promise.reject('请选择开始时间和结束时间');
	}
	return Promise.resolve();
};


/**
 * @description: 合并单元格数据
 * @return {*}
 */

export const mergeTableList = (list: any[], expandKey: string) => {
	const result = [];
	for (const item of list) {
		const expandList = item[expandKey] || [];
		if (!expandList.length) {
			item.span = 1;
			result.push(item);
		} else {
			const len = expandList.length;
			expandList.forEach((element: any, i: number) => {
				result.push({
					...element,
					...item,
					span: i === 0 ? len : 0,
				});
			});
		}
	}
	return result;
};

/**
 * @description: 处理单元格合并
 * @return {*}
 */
export const getSharedCell = (record: any) => {
	return {
		rowSpan: record?.span,
	}
};

/**
 * @description: 服务项配置table columns
 * @return {*}
 */

export const getServiceConfigColumns = (noMerge?: boolean) => [
	{
		title: "名称",
		dataIndex: 'Name',
		customCell: noMerge ? null : getSharedCell,
	},
	{
		title: '标签',
		dataIndex: 'Tag',
		customCell: noMerge ? null : getSharedCell,
	},
	{
		title: '适用等级',
		dataIndex: 'Level',
	},
	{
		title: '方式',
		dataIndex: 'Way',
	},
	{
		title: '描述',
		dataIndex: 'Desc',
	},
	// {
	// 	title: '时间',
	// 	dataIndex: 'time',
	// },
	{
		title: '操作',
		dataIndex: 'operation',
		width: '105px',
		customCell: noMerge ? null : getSharedCell,
	},
];

/**
 * @description: 服务包配置
 * @param {any} configs
 * @return {*}
 */
export const getServiceContent = (configs: ServiceContentConfig[]): ServiceTagConfig[] => {
	const result: ServiceTagConfig[] = [];
	for (const item of configs) {
		const list = item.checkedList || [];
		for (const id of list) {
			result.push({
				// levelID: Number(base.levelID),
				settingID: id,
				settingLevelID: item.settingIdMap[id],
				tagID: item.tagID || 0,
			});
		}
	}
	return result;
}

